<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/24
 * Time: 15:55
 */

namespace Bbs\Api;

use Bbs\Model\CoreReportModel;
use Bbs\Model\VUserInfoModel;
use Common\Api\Api;

class CoreReportApi extends Api{

    /**
     * 添加
     */
    const ADD = "Bbs/CoreReport/add";
    /**
     * 保存
     */
    const SAVE = "Bbs/CoreReport/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Bbs/CoreReport/saveByID";

    /**
     * 删除
     */
    const DELETE = "Bbs/CoreReport/delete";

    /**
     * 查询
     */
    const QUERY = "Bbs/CoreReport/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Bbs/CoreReport/getInfo";

    /**
     * 插入多条数据
     */
    const ADD_ALL = "Bbs/CoreReport/addAll";

    /**
     * 无分页查询
     */
    const QUERY_NO_PAGING = "Bbs/CoreReport/queryNoPaging";


    protected function _init(){
        $this -> model = new CoreReportModel();
    }

    public function query($map = null, $page = array('curpage' => 0, 'size' => 10), $order = false, $params = false, $fields = false)
    {
        $query = $this->model ->alias('c');
        if(!is_null($map)){
            $query = $query->where($map)
                ->join('left join common_member as m on m.uid = c.op_uid');
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }
        $list = $query -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();
        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }
}